#include "Precompile.h"
#include "BoostThread.h"

namespace ThreadLib{
	BoostThread::BoostThread(boost::function<void(void)> function):Thread(function),m_running(false){
	}

	BoostThread::~BoostThread(){
		if(isRunning()){
			LOG_ERROR("BoostThread::~BoostThread - Destruction of thread object while thread is running.");
			assert(0);
			join();
		}
	}

	void BoostThread::join(){
		m_thread.join();
	}

	void BoostThread::run(){
		if(isRunning()){
			LOG_ERROR("BoostThread::run() - Called on thread when already running");
			assert(0);
			join();
		}

		m_thread = boost::thread(boost::function<void(void)>(boost::bind(&BoostThread::executionFunction,this)));
	}

	bool BoostThread::isRunning(){
		return m_running;
	}

	void BoostThread::executionFunction(){
		m_running = true;
		m_function();
		m_running = false;

		ThreadFactory::releaseThread(shared_from_this());
	}
}